package com.zhida.jiyangcenter.mian

import android.animation.Animator
import android.annotation.SuppressLint
import android.os.Build
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.lifecycle.lifecycleScope
import chooongg.base.activity.QuickActivity
import chooongg.base.annotation.TitleBar
import chooongg.base.manager.app
import chooongg.base.utils.decorView
import chooongg.base.utils.gone
import chooongg.base.utils.hideSoftInput
import chooongg.base.widget.backlayout.ParallaxBackEnable
import chooongg.base.widget.backlayout.ViewDragHelper
import chooongg.base.widget.titleBar.CenterTitleBar
import com.airbnb.lottie.LottieAnimationView
import com.airbnb.lottie.LottieDrawable
import com.zhida.jiyangcenter.R
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@TitleBar(true, true, CenterTitleBar::class)
@ParallaxBackEnable(true)
open class BaseActivity : QuickActivity() {

    // 只剩一个页面时启动双击返回
    //记录用户首次点击返回键的时间
    private var firstTime: Long = 0
    private var loadingTip: View? = null

    override fun onBackPressed() {
        if (app.activityPrevious == null) {
            val secondTime = System.currentTimeMillis()
            if (secondTime - firstTime > 2000) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show()
                firstTime = secondTime
            } else super.onBackPressed()
        } else super.onBackPressed()
    }
    fun showTipLoading(@StringRes resId: Int) {
        showTipLoading(getString(resId))
    }

    @SuppressLint("InflateParams")
    fun showTipLoading(message: CharSequence? = null, isClickable: Boolean = false) {
        lifecycleScope.launch(context = Dispatchers.Main) {
            if (loadingTip != null) {
                if (message.isNullOrEmpty()) {
                    loadingTip!!.findViewById<TextView>(R.id.tv_message).gone()
                } else {
                    loadingTip!!.findViewById<TextView>(R.id.tv_message).text = message
                }
                return@launch
            }
            loadingTip = layoutInflater.inflate(R.layout.view_tip, null)
            loadingTip!!.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
            loadingTip!!.findViewById<LottieAnimationView>(R.id.view_lottie).apply {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
                    gone()
                    return@apply
                }
                setAnimation(R.raw.lottie_tip)
                setMinAndMaxFrame(0, 239)
                repeatMode = LottieDrawable.RESTART
                repeatCount = LottieDrawable.INFINITE
                speed = 1.5f
                playAnimation()
            }
            if (message.isNullOrEmpty()) {
                loadingTip!!.findViewById<TextView>(R.id.tv_message).gone()
            } else {
                loadingTip!!.findViewById<TextView>(R.id.tv_message).text = message
            }
            if (!isClickable) loadingTip!!.setOnClickListener { }
            loadingTip!!.alpha = 0f
            decorView.addView(loadingTip)
            loadingTip!!.animate().alpha(1f)
        }
    }

    fun hideTipLoading() {
        if (!isDestroyed) {
            lifecycleScope.launch(context = Dispatchers.Main) {
                if (loadingTip == null) return@launch
                loadingTip!!.animate().alpha(0f).setListener(object : Animator.AnimatorListener {
                    override fun onAnimationStart(animation: Animator?) {
                    }

                    override fun onAnimationRepeat(animation: Animator?) {
                    }

                    override fun onAnimationCancel(animation: Animator?) {
                        decorView.removeView(loadingTip)
                        loadingTip = null
                    }

                    override fun onAnimationEnd(animation: Animator?) {
                        decorView.removeView(loadingTip)
                        loadingTip = null
                    }
                })
            }
        }
    }

}